package com.sat.slidemenu;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Project:Wisdom
 * Package:com.sat.slidemenu
 * Created by 透野君爱打奶炮 on 2018-04-20 at 17:56
 */
public class SlideMenu extends ViewGroup {
    /** 按下的x*/
    private float downX;
    /** 按下的y*/
    private float downY;
    private boolean menu_mode = false;
    private Scroller scroller;

    public SlideMenu(Context context) {
        this(context, null);
    }

    public SlideMenu(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideMenu(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        scroller = new Scroller(getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View menuLeft = getChildAt(0);
        menuLeft.measure(menuLeft.getLayoutParams().width, heightMeasureSpec);

        View contentRight = getChildAt(1);
        contentRight.measure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View menuLeft = getChildAt(0);
        menuLeft.layout(-menuLeft.getMeasuredWidth(), 0, 0, b);
        getChildAt(1).layout(l, t, r, b);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = ev.getX();
                float moveY = ev.getY();
                float xOffset = Math.abs(moveX-downX);
                float yOffset = Math.abs(moveY-downY);
                if (xOffset > yOffset && xOffset>5){
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("TAG","按下");

                downX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("TAG","滑动");
                float moveX = event.getX();

                /** 将要滚动的距离 */
                int offset = (int) (downX - moveX);
                int scrollX = getScrollX() + offset;
                if (scrollX>0){
                    //超出正文
                    scrollTo(0,0);
                }else if (scrollX<-getChildAt(0).getWidth()){
                    scrollTo(-getChildAt(0).getWidth(),0);
                }else {
                    scrollBy(offset,0);
                }
                downX = moveX;
                //scrollTo();
                //return true;

                break;
            case MotionEvent.ACTION_UP:
                //float upX = event.getX();
                int upX = getScrollX();
                if (upX<-0.5*getChildAt(0).getWidth()){
                    //弹出menu
                    menu_mode = true;
                    updateCurrentMode();
                }else {
                    //缩回正文
                    menu_mode = false;
                    updateCurrentMode();
                }
                break;
        }
        return true;
    }

    private void updateCurrentMode() {
        int startX = getScrollX();
        int dx = 0;
        if (menu_mode){
            Log.d("TAG","menu_mode");
            dx = -getChildAt(0).getWidth() - startX;
        }else {
            Log.d("TAG","content_mode");
            dx = -startX;
        }

        scroller.startScroll(startX,0,dx,0);

        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()){
            int currX = scroller.getCurrX();
            scrollTo(currX,0);
        }
        invalidate();
    }

    public void switchState(){
        if (menu_mode){
            menu_mode = false;
        }else {
            menu_mode = true;
        }

        updateCurrentMode();
    }
}
